import java.io.IOException;
import javax.swing.JScrollPane;

import javax.swing.BoxLayout;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

//import javax.swing.*;
//import javax.swing.event.*;
//import javax.swing.text.*;

@SuppressWarnings("serial")

public class TagSearchPanel extends Panel{

	//private TextField entry = new TextField();
	private TextArea disp = new TextArea(); 
	//private Button searchBt = new Button("Search");
	//private Button clearBt = new Button("Clear");
	public String s = new String();
	public List<String> tList = new ArrayList<String>();
	
    public ArrayList<String> picturelist2 =new ArrayList<String>();

	public TagSearchPanel(){
		
		//clearBt.addActionListener(new cB());
		//searchBt.addActionListener(new sB());
		/* 
		entry.setVisible(true);
		entry.setEditable(true);
		entry.setText("Enter Query Tag\n");	
		this.add(entry);
		*/
		//this.add(searchBt);
		//this.add(clearBt);
//		this.add(disp);
		
	}
	
	/*
			 class cB implements ActionListener {
				 public void actionPerformed(ActionEvent e) {
				  tList.clear();
			      s = "" ;
			      entry.setText(s);
			      disp.setText(s);
	 		      // Re-initialize the display to original - Call the First function 
			}			
		 }
		*/ 
			 /*
			 class sB implements ActionListener {
				 public void actionPerformed(ActionEvent e) {
					 
					 String temp = entry.getText();
				      if (temp == null)
				    	  return;
				      
				      tList.add(temp) ;
				      s = s + ',' + temp;
				      disp.setText(s);
				      entry.setText("");
				      try{
				    	  search();
				      } catch(Exception err1)
				      {
				    	  System.err.println(err1.getMessage());
				      }
			}

*/
			 
			 
	public void clickedTagAdd(String newTag) {
	// Please call this function when a tag on the bar chart or pie chart clicked.
		tList.add(newTag);
		s = s + "," + newTag; 
		disp.setText(s);
		//entry.setText("");
		try{
			search();
		}catch(Exception err1)
	      {
	    	  System.err.println(err1.getMessage());
	      }
	}
	
	public void search() throws SQLException, ParseException { 	 
	      
		 String tagList = new String("");
		 int len = tList.size(); 
		 for (int i = 0 ; i < len - 1 ; i++) {
			 tagList = tagList + '"' + tList.get(i) + '"' + ','; 
		 }
		 tagList = tagList + '"' + tList.get(len-1) + '"';
		      
		//Form sql connection and call updates to other displays	      
		disp.setText("Query: SELECT image_id from images where tag in ("+tagList+")");
		
		 
		System.out.print("Query: SELECT unique image_id from images where tag in ("+tagList+")");
		
		List<String> uList = new ArrayList<String>();
		List<String> imList = new ArrayList<String>();
		String imStr = new String();
		
		
		Connection conn = DriverManager.getConnection("jdbc:mysql://ist402.ist.psu.edu/nks125?user=nks125&password=qwert123y");
		if(conn != null){
		// Get relevant image ids 
		try {
			Statement stmt = conn.createStatement();
			String sql = "SELECT image_id from tags where tag in ("+tagList+")";
				
			ResultSet rs = stmt.executeQuery(sql);
			String e = new String();
			while(rs.next()){
				e = rs.getString("image_id");
				imList.add(e);
				imStr = imStr + '"' + e + '"' + ',';
			}
			imStr = imStr + '"' + e + '"' ;
			rs.close();
			stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
			}
		
		//System.out.print("finished reading images "+imStr);
		
		//System.out.print("finished reading images hahahaha ");
		
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		// Update user, tag, date list 
		try {
			Statement stmt = conn.createStatement();
			StringBuffer sql = new StringBuffer();
			sql.append("SELECT * from images where image_id in ("+imStr+")");
			
			
			Date startDate = new Date(0000-00-00);
			Date endDate = new Date(2000-12-31);
			ResultSet rs = stmt.executeQuery(sql.toString());
			int start = 1;
			String e = "0000-00-00";
			
			picturelist2.clear();
			System.out.print("cant see the light");
			while(rs.next()){
				
				// reads users 
				e = rs.getString("owner");
				uList.add(e);
					
				// read url
				picturelist2.add(rs.getString("url"));
				
				
			System.out.print(rs.getString("url\n"));
			// start and end date 	
				if (start==1)
				{
					try{
						startDate = (Date)df.parse(e);
					}catch (ParseException e2){						
						e2.printStackTrace();
					}
				}	
			}
			
			try{
				endDate = (Date) df.parse(e);
			}catch (ParseException e2){						
				e2.printStackTrace();
			}
						
			stmt.close();
			rs.close();
		} catch (SQLException e) {
				e.printStackTrace();
			}
			
 	}
 	
 	// call functions to update other displays
		
	}	
	

	public static void main(String[] args) {
		javax.swing.JFrame frame = new javax.swing.JFrame();
		ArrayList<String> myArr = new ArrayList<String>();
        myArr.add("http://www.gstatic.com/hostedimg/ef3fe9b49b289ee2_landing");
        myArr.add("http://www.iacuc.arizona.edu/training/cats/images/Tabby1-DomesticCat-Closeup.jpg");
        myArr.add("http://www.gstatic.com/hostedimg/ef3fe9b49b289ee2_landing");
        myArr.add("http://www.iacuc.arizona.edu/training/cats/images/Tabby1-DomesticCat-Closeup.jpg");
        myArr.add("http://www.gstatic.com/hostedimg/ef3fe9b49b289ee2_landing");
        myArr.add("http://www.iacuc.arizona.edu/training/cats/images/Tabby1-DomesticCat-Closeup.jpg");
        
//        
//        
		TagSearchPanel tp = new TagSearchPanel();
		frame.getContentPane().add(tp);
		frame.setSize(300, 500);
		frame.setVisible(true);
	}
	
}